<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<script>
    function add() {
        document.forms[0].action="<%=request.getContextPath()%>/customer/addCustomer.jsp";
        document.forms[0].submit();
    }
  
    function del() {
        if (confirm("Are you sure want to delete?")) {
            document.forms[0].action="<%=request.getContextPath()%>/delCustomers";
            document.forms[0].submit();
        } 
    }
    
    function toggle(flg) {
        var allChecks = document.getElementsByName("check");

        for each(var chkbox in allChecks) {
            chkbox.checked = flg;
        }
    }

    function checkAll() {
        if (document.getElementById("chkall").checked) {
            toggle(true);
        } else {
            toggle(false);
        }
    }
</script>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="<%=request.getContextPath()%>/getCustomerList">
            <input type="hidden" name="hidCount" value="${hidCount}">
            <input type="text" name="_name">
            <input type="submit" value="Search">
            <c:if test="${not empty customer_err}">
                <div style="color:red"><c:out value="${customer_err}"/></div>
            </c:if>
            <c:if test="${custList != null}">
                <c:if test="${not empty customer_msg}">
                    <div style="color:blue"><c:out value="${customer_msg}"/></div>
                </c:if>

                <c:if test="${not empty customer_msg_2}">
                    <div style="color:blue"><c:out value="${customer_msg_2}"/></div>
                </c:if>
                <table border="1">
                    <tr>
                        <th>Customer Id</th>
                        <th>Customer name</th>
                        <th><input type="checkbox" id="chkall" onclick="checkAll()" value="false"></th>
                    </tr>
                    <c:forEach items="${custList}" var="cust">
                        <tr>
                            <td>
                                <a href="<%=request.getContextPath()%>/viewCustDetail?id=<c:out value='${cust.custId}'/>">
                                    <c:out value='${cust.custId}'/>
                                </a>
                            </td>
                            <td><c:out value="${cust.custName}"/></td>
                            <td><input type="checkbox" name="check" value="${cust.custId}"></td>
                        </tr>
                    </c:forEach>
                </table>
            </c:if>
            <br/>
            <c:forEach items="${pages}" var="p">
                <a href="<%=request.getContextPath()%>/getCustomerList?attr_name=${name}&page=${p}">${p}</a>
            </c:forEach>
            <BR>
            <input type="button" value ="Add" onclick="add()">
            <input type="button" value ="Delete" onclick="del()">
        </form>
    </body>
</html>
